<template>
  <div class="employees">
    <el-card class="btn">
      <el-button type="primary">导入</el-button>
      <el-button type="primary" @click="add">+ 新增员工</el-button>
    </el-card>
    <el-card>
      <el-table :data="list">
        <!-- correctionTime: "2018-11-30"
: "总裁办"
: 1
: 1
id: "604f764971f93f3ac8f365c2"
mobile: "13800000002"
password: "e10adc3949ba59abbe56e057f20f883e"
staffPhoto: "http://q6cu3t6jv.bkt.clouddn.com/1063705989926227968?t=1616204161907"
: "2018-11-02"
username: "管理员" -->
        <!-- workNumber: "9002" -->
        <el-table-column label="序号">
          <template v-slot="{ $index }">
            <div>
              {{ (page.page - 1) * page.size + $index + 1 }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="姓名" prop="username" />
        <el-table-column label="手机号" prop="mobile" />
        <el-table-column label="工号" prop="workNumber" />
        <el-table-column label="聘用形式" prop="formOfEmployment" />
        <el-table-column label="部门" prop="departmentName" />
        <el-table-column label="入职时间" prop="timeOfEntry">
          <template v-slot="{ row }">
            <!-- {{ row }} -->
            <div>
              {{ row.timeOfentry | formatTime }}
            </div>
          </template>
        </el-table-column>
        <!-- <el-table-column label="是否在职" prop="" /> -->
        <el-table-column label="状态" prop="enableState" />
        <el-table-column label="操作" width="300" fixed="right" class="cz">
          <template v-slot="{ row }">
            <div>
              <el-button type="text">查看</el-button>
              <el-button type="text">转正</el-button>
              <el-button type="text">调岗</el-button>
              <el-button type="text">离职</el-button>
              <el-button type="text">角色</el-button>
              <el-button type="text" @click="del(row.id)">删除 </el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="page">
        <el-pagination
          :current-page="page.page"
          :page-sizes="[1, 10, 100, 200, 300, 400]"
          :page-size="page.size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="page.total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
    <Dialog :is-show="show" @update:isShow="show = $event" @getData="getData" />
  </div>
</template>

<script>
import { sysUser, sysUserDel } from '@/api/employees'
import Dialog from './components/dialog.vue'
export default {
  components: {
    Dialog
  },
  data() {
    return {
      show: false,
      list: [],
      page: {
        total: 100,
        page: 1,
        size: 1
      }
    }
  },
  created() {
    this.getData()
  },
  methods: {
    async getData() {
      const res = await sysUser(this.page)
      this.list = res.data.rows
      // res.data.rows.forEach((item) => {
      //   item.formOfEmployment = this.formatter(
      //     null,
      //     null,
      //     item.formOfEmployment
      //   )
      // })
      this.page.total = res.data.total
      console.log(res)
    },
    handleSizeChange(size) {
      // 页容量
      this.page.size = size
      this.page.page = 1
      this.getData()
    },
    handleCurrentChange(page) {
      // 页码
      this.page.page = page
      this.getData()
    },
    // 新增员工
    add() {
      this.show = true
    },
    // 删除员工
    del(id) {
      // 1 删除提示
      this.$confirm('确定删除吗', '提示').then(async() => {
        // 2 定义接口  导入 调用
        await sysUserDel(id)
        // 3 提示用户
        this.$message.success('删除成功')
        // 4 回到第一页
        this.handleCurrentChange(1)
        // 5 刷新数据
        this.getData()
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.employees {
  padding: 15px;
  .btn {
    text-align: right;
    margin-bottom: 15px;
  }
  .page {
    padding-top: 30px;
    text-align: right;
  }
  .cz {
    text-align: center;
  }
}
</style>
